home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / demos / glxpixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-03  |  3.3 KB  |  143 lines

  1. /* glxpixmap.c */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLXPixmap functions.  This program is in
  6.  * the public domain.
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11.  
  12. #include <GL/gl.h>
  13. #include <GL/glx.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. static GLXContext ctx;
  18. static XVisualInfo *visinfo;
  19. static GC gc;
  20.  
  21.  
  22.  
  23. static Window make_rgb_window( Display *dpy,
  24.                   unsigned int width, unsigned int height )
  25. {
  26.    int attrib[] = { GLX_RGBA,
  27.             GLX_RED_SIZE, 1,
  28.             GLX_GREEN_SIZE, 1,
  29.             GLX_BLUE_SIZE, 1,
  30.             None };
  31.    int scrnum;
  32.    XSetWindowAttributes attr;
  33.    unsigned long mask;
  34.    Window root;
  35.    Window win;
  36.  
  37.    scrnum = DefaultScreen( dpy );
  38.    root = RootWindow( dpy, scrnum );
  39.  
  40.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  41.    if (!visinfo) {
  42.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  43.       exit(1);
  44.    }
  45.  
  46.    /* window attributes */
  47.    attr.background_pixel = 0;
  48.    attr.border_pixel = 0;
  49.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  50.    attr.event_mask = StructureNotifyMask | ExposureMask;
  51.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  52.  
  53.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  54.                 0, visinfo->depth, InputOutput,
  55.                 visinfo->visual, mask, &attr );
  56.  
  57.    /* make an X GC so we can do XCopyArea later */
  58.    gc = XCreateGC( dpy, win, 0, NULL );
  59.  
  60.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  61.  
  62.    return win;
  63. }
  64.  
  65.  
  66. static GLXPixmap make_pixmap( Display *dpy, Window win,
  67.                    unsigned int width, unsigned int height )
  68. {
  69.    Pixmap pm;
  70.    GLXPixmap glxpm;
  71.  
  72.    pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
  73.  
  74.    glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
  75.  
  76.    return glxpm;
  77. }
  78.  
  79.  
  80.  
  81. static void event_loop( Display *dpy, GLXPixmap pm )
  82. {
  83.    XEvent event;
  84.  
  85.    while (1) {
  86.       XNextEvent( dpy, &event );
  87.  
  88.       switch (event.type) {
  89.      case Expose:
  90.         printf("Redraw\n");
  91.         /* copy the image from GLXPixmap to window */
  92.         XCopyArea( dpy, pm, event.xany.window,  /* src, dest */
  93.                gc, 0, 0, 300, 300,          /* gc, src pos, size */
  94.                0, 0 );                      /* dest pos */
  95.         break;
  96.      case ConfigureNotify:
  97.         /* nothing */
  98.         break;
  99.       }
  100.    }
  101. }
  102.  
  103.  
  104.  
  105. main( int argc, char *argv[] )
  106. {
  107.    Display *dpy;
  108.    Window win;
  109.    GLXPixmap pm;
  110.  
  111.    dpy = XOpenDisplay(NULL);
  112.  
  113.    win = make_rgb_window( dpy, 300, 300 );
  114.    pm = make_pixmap( dpy, win, 300, 300 );
  115.  
  116.    /* *VERY IMPORTANT*:  In Mesa's implementation of GLX, a GLXContext should
  117.     * first be bound to a window, just temporarily, before it can be bound to a
  118.     * GLXPixmap.  The reason is we sometimes (when a PsuedoColor visual is
  119.     * used in RGB mode) need to know the colormap associated with the context.
  120.     * The colormap can't be discovered from a pixmap but can be found with a
  121.     * window.
  122.     */
  123.  
  124.    glXMakeCurrent( dpy, win, ctx );  /*to make sure ctx is properly initialized*/
  125.    glXMakeCurrent( dpy, pm, ctx );
  126.  
  127.    /* Render an image into the pixmap */
  128.    glShadeModel( GL_FLAT );
  129.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  130.    glClear( GL_COLOR_BUFFER_BIT );
  131.    glViewport( 0, 0, 300, 300 );
  132.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  133.    glColor3f( 0.0, 1.0, 1.0 );
  134.    glRectf( -0.75, -0.75, 0.75, 0.75 );
  135.    glFlush();
  136.  
  137.    /* when a redraw is needed we'll just copy the pixmap image to the window */
  138.  
  139.    XMapWindow( dpy, win );
  140.  
  141.    event_loop( dpy, pm );
  142. }
  143.